<%--
   treeMenu
   Copyright (c) 2013 ISPERP
   Version : 1.0.0
 --%>
<%@ tag language="java" pageEncoding="UTF-8"
	trimDirectiveWhitespaces="true"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@ attribute name="id" required="true" type="java.lang.String"
	description="唯一标识."%>


<%@ attribute name="targetGridId" required="false"
	type="java.lang.String" description="目标的grid的唯一标识"%>

<%@ attribute name="title" required="false" type="java.lang.String"
	description="标题"%>

<%@ attribute name="cssClass" required="false" type="java.lang.String"
	description="class."%>

<%@ attribute name="cssStyle" required="false" type="java.lang.String"
	description="stlye."%>

<%@ attribute name="nodes" required="false" type="java.lang.String"
	description=""%>
	
<c:set var="async" scope="request" />
<c:set var="check" scope="request" />
<c:set var="callback" scope="request" />
<c:set var="data" scope="request" />
<jsp:doBody />

<c:if test="${empty async}">
	<c:set var="async" value="{}" scope="request" />
</c:if>
<c:set var="datatype" value="local" scope="request" />
<c:if test="${empty check}">
	<c:set var="check" value="{}" scope="request" />
</c:if>
<c:if test="${empty edit}">
	<c:set var="edit" value="{}" scope="request" />
</c:if>
<c:if test="${empty view}">
	<c:set var="view"
		value="{'showIcon':false,'showLine':false,nameIsHTML:true}"
		scope="request" />
</c:if>
<c:if test="${empty callback}">
	<c:set var="callback" value="{}" scope="request" />
</c:if>
<c:if test="${empty data}">
	<c:set var="data" value="{}" scope="request" />
</c:if>
<c:if test="${empty nodes}">
	<c:set var="nodes" value="null" scope="request" />
</c:if>

<c:set var="setting" scope="request"
	value="{'async':${async},'check':${check},'view':${view},'edit':${edit},callback:${callback },data:${data }},${nodes }" />

<c:if test="${empty title}">
	<c:set var="showTitle" value="hidden" scope="request" />
</c:if>
<div class="treePanel box no-border">
	<div class="header ${showTitle}">
		<h2>${title}</h2>
	</div>
	<ul id="${id}" class="ztree menutree ${cssClass }" style="${cssStyle}"></ul>
</div>
<jsp:doBody />

<script type="text/javascript">
function ${id}Tree_onNodeClick(event, treeId,treeNode) {
	if(${targetGridId}.jqGrid){
		var url =  ${targetGridId}.jqGrid.getGridParam("url"); 
		url=url.replaceUrlParam("queryPath", encodeURIComponent(treeNode.treenodePath)); 
		${targetGridId}.jqGrid.setGridParam({url:url});
		${targetGridId}.search();
	}
} 
function ${id}Tree_onNodeClickFirst(event, treeId, msg) {
	if(${targetGridId}.jqGrid){
		var node =${id}.beforeReloadSelectedNode;
		if(node==null||node==undefined){
			var nodes=${id}.zTree.getNodes();
			if(nodes&&nodes[0]){
				node=nodes[0];
			}else{return;}
		
		}
		${id}.zTree.selectNode(node);
		var url =  ${targetGridId}.jqGrid.getGridParam("url"); 
		url=url.replaceUrlParam("queryPath", encodeURIComponent(node.treenodePath)); 
	
		${targetGridId}.jqGrid.setGridParam({url:url});
		${targetGridId}.search();
	}
}

${id}.zTree = $.fn.zTree.init($("#${id}"), ${setting});

${id}.getSelectedNode=function(){
	var nodes = ${id}.zTree.getSelectedNodes();	 
	if(nodes==undefined)
		return undefined;	
	return nodes[0];
};
${id}.beforeReloadSelectedNode=null;
${id}.reload = function(){
	${id}.beforeReloadSelectedNode=${id}.getSelectedNode()
	${id}.zTree.reAsyncChildNodes(null, "refresh");
};
${id}.afterAsyncSuccess=function(event, treeId, treeNode, msg){
	if(${id}.beforeReloadSelectedNode){
		var nnode= ${id}.zTree.getNodeByParam("id",${id}.beforeReloadSelectedNode.id);
		if(nnode)
			${id}.zTree.selectNode(nnode, false);
	}
};



</script>

